package org.remixlab.gnutopic.client;

import org.remixlab.gnutopic.pojo.ForumThread;

import processing.core.PVector;
import remixlab.proscene.Camera;
import remixlab.proscene.InteractiveFrame;
import remixlab.proscene.Scene;

public class ThreadIFrame extends InteractiveFrame {
	private float halfwidth, height; // dimensiones del cilindro.
	
	private PVector pos, rVec;
	  
	private ForumThread parent;
	
	private boolean selected = false;
	 
	public ForumThread getParent() {
		return parent;
	}

	public void setParent(ForumThread parent) {
		this.parent = parent;
	}

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public ThreadIFrame(Scene scn, float w, float h) {
	    super(scn);
	    halfwidth = w;
	    height = h;
	  }

	  public void checkIfGrabsMouse(int x, int y, Camera camera) {
	    pos = position();
	    rVec = camera.rightVector();
	  
	    PVector leftUpCorner = PVector.add(pos, PVector.mult(zAxis(), height));
	    leftUpCorner.add(PVector.mult(rVec, -halfwidth));
	    PVector rightDownCorner = PVector.add(pos, PVector.mult(rVec, halfwidth));
	           
	    PVector leftProj = camera.projectedCoordinatesOf(leftUpCorner);
	    PVector rightProj = camera.projectedCoordinatesOf(rightDownCorner);
	    
	    float delta1 = Math.abs(rightProj.x - leftProj.x);
	    float delta2 = Math.abs(leftProj.y - rightProj.y);
	    
	    if(delta2 > delta1)
	      setGrabsMouse(keepsGrabbingMouse || ( ( 
	                                         ( x >= leftProj.x ) && ( x <= rightProj.x )
	                                         &&
	                                         ( y >= leftProj.y ) && ( y <= rightProj.y )
	                                         ) ) );
	    else {
	      setGrabsMouseThreshold(Math.round(delta1/2));
	      super.checkIfGrabsMouse(x,y,camera);
	    }
	  }
	  
	  public void mouseClicked(Scene.Button button, int numberOfClicks, Camera camera){
		  if(parent!=null){
			  if(numberOfClicks==2){
				System.out.println(parent.toString());
				
				selected = true;
			  }  
		  }
	  }
	  
}
